package appnode;

import javax.jdo.PersistenceManager;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.util.List;
import java.util.Date;
import java.util.ArrayList;
import appnode.Node;
import appnode.SearchNode;
import appnode.Link;
import appnode.Interface;
import javax.jdo.Query;
import java.lang.*;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class Linktools {
	
	// no other link between nodes
	public static String validlink(Node node1, Node node2) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();	
		String result = "yes";
		String keystr = "";
		Key keylink;
		Interface if2;
		
	
			for (int i=0;i<node1.getInterfaces().size();i++) {
				if (node1.getInterfaces().get(i).getFrominterface()!=null && !node1.getInterfaces().get(i).getFrominterface().equals("")) {	
					keylink = KeyFactory.stringToKey(node1.getInterfaces().get(i).getFrominterface());
					if2 = pm.getObjectById(Interface.class,keylink);
					keystr = KeyFactory.keyToString(node1.getInterfaces().get(i).getKey());
					if (if2.getFrominterface()!=null) {
						if (if2.getFrominterface().equals(keystr) && if2.getNode().getNodename().equals(node2.getNodename())) {
							result = "no";
						}
					} // if
				} // if
			}
			return result;
	}
				
	
	public static String containif(String if1, String if2) {
		
		String result = "no";
		PersistenceManager pm = PMF.get().getPersistenceManager();	
		int i = 0;
		Key keyif1 = KeyFactory.stringToKey(if1);
	
		Interface i1 = pm.getObjectById(Interface.class,keyif1);
	
		result = "no";
		
		// if1 contains if2
		for (i=0;i<i1.getNode().getInterfaces().size();i++) {
			if (i1.getNode().getInterfaces().get(i).getLink().equals(if2)) {
				result = "yes";
			}
		}
		return result;
	}
	
	
	
	/*public static String validif(String name1, name2) {
		
		String valid = "";
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Link.class);
		//query.setFilter("nodename1=='name1' || nodename=='name2'");
		List<Link> results = (List<Link>) query.execute();
		if (results.isEmpty()) {
			valid = "null";
		}
		else {
			;
		}
		return valid;
	}*/
}